/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
 * 通用工具类
 *
 * @author Chill
 */
public class CommonUtil {

	private static final String[] CN_NUM_ARRAY ={"零","一","二","三","四","五","六","日","八","九"};
	private static final String[] CN_NUM_ARRAY1 ={"","日", "一","二","三","四","五","六" };

	public static String numZh(int num){
		if(num<0|| num >9){
			return "";}
		else{
			return CN_NUM_ARRAY[num];
		}
    }
	public static String numZhEx(int num){
		if(num<0|| num >9){
			return "";}
		else{
			return CN_NUM_ARRAY1[num];
		}
	}
	public static int calculateAge(Date birthDate, Date currentDate) {
		Calendar birthCalendar = Calendar.getInstance();
		birthCalendar.setTime(birthDate);
		Calendar currentCalendar = Calendar.getInstance();
		currentCalendar.setTime(currentDate);
		int age = currentCalendar.get(Calendar.YEAR) - birthCalendar.get(Calendar.YEAR);
		if (currentCalendar.get(Calendar.MONTH) < birthCalendar.get(Calendar.MONTH)
			|| (currentCalendar.get(Calendar.MONTH) == birthCalendar.get(Calendar.MONTH)
			&& currentCalendar.get(Calendar.DAY_OF_MONTH) < birthCalendar.get(Calendar.DAY_OF_MONTH))) {
			age--;
		}
		return age;
	}

	public static List<String>  get1Days (String startDate, String endDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		//开始
		LocalDate localStartDate = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		//结束
		LocalDate localEndDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		//间隔    包含当天需要加1
		long numOfDays = ChronoUnit.DAYS.between(localStartDate, localEndDate) + 1L;
		List<LocalDate> listOfDates1 = LongStream.range(0, numOfDays)
			.mapToObj(localStartDate::plusDays)//映射
			.collect(Collectors.toList());//收集

		List<String> listdays = new ArrayList<>();
		listOfDates1.forEach(item->{
			listdays.add(item.format(df));
		});

		return listdays;
	}

	public static List<String>  getDays (Date sDate, Date eDate){

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String startDate = sdf.format(sDate) ;
		String endDate = sdf.format(eDate) ;

		//开始
		LocalDate localStartDate = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		//结束
		LocalDate localEndDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		//间隔    包含当天需要加1
		long numOfDays = ChronoUnit.DAYS.between(localStartDate, localEndDate) + 1L;
		List<LocalDate> listOfDates1 = LongStream.range(0, numOfDays)
			.mapToObj(localStartDate::plusDays)//映射
			.collect(Collectors.toList());//收集

		List<String> listdays = new ArrayList<>();
		listOfDates1.forEach(item->{
			listdays.add(item.format(df));
		});

		return listdays;

	}

	public static List<String>  getWeekDays (Date sDate, Date eDate, DayOfWeek dayOfWeek){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String startDate = sdf.format(sDate) ;
		String endDate = sdf.format(eDate) ;
		//开始
		LocalDate localStartDate = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		//结束
		LocalDate localEndDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		// 打印两个日期段内的所有周一
		LocalDate date = localStartDate;
		List<LocalDate> listOfDates1 = new ArrayList<>();
		while (!date.isAfter(localEndDate)) {
			if (date.getDayOfWeek() == dayOfWeek  ) {
				listOfDates1.add(date);
			}
			date = date.plusDays(1);
		}
		List<String> listdays = new ArrayList<>();
		listOfDates1.forEach(item->{
			listdays.add(item.format(df));
		});

		return listdays;
	}


	public static void main(String[] args) {

		get1Days("2023-06-01","2023-06-30" ).forEach(item->{

				item = item+" "+"09:00"+":00";
				System.out.println(item);


		});
	}

}
